<template>
	<view class="safety-analysis-page">
		<!-- 顶部导航栏 -->
		<u-navbar :placeholder="true" leftIcon=""></u-navbar>
		<image class="head-bg" src="/static/images/headBg.jpg" alt=""></image>

		<scroll-view scroll-y class="page-content">
			<view class="header">
				<view class="header-left">
					<text>综合分析</text>
					<text>数据更新: {{getCurrentDate()}}</text>
				</view>
				<view class="header-right">
					<view class="date-selector" @click="selectDate">
						<text class="date-text">{{showTime}}年</text>
						<u-icon name="arrow-down-fill" size="10" color="#666"></u-icon>
					</view>
				</view>
			</view>
			<!-- KPI指标卡片 -->
			<view class="kpi-section" style="padding: 30rpx 16rpx 0 16rpx;">
				<view class="bg-box">
					<image class="index-bg" src="@/static/images/indexBg4.png" mode=""></image>
					<view class="kpi-row-head">
						<view class="kpi-card large">
							<view class="kpi-header">
								<!-- <image src="/static/images/yearOne.jpg"></image> -->
								<u-icon name="/static/images/index/product.svg" size="20"></u-icon>
								<text class="kpi-title">本年生产</text>
							</view>
							<text class="kpi-value">{{yearData.product}}</text>
						</view>
						<view class="kpi-card large">
							<view class="kpi-header">
								<!-- <image src="/static/images/yearOne.jpg"></image> -->
								<u-icon name="/static/images/index/sale.svg" size="20" color="#1890ff"></u-icon>
								<text class="kpi-title">本年销售</text>
							</view>
							<text class="kpi-value">{{yearData.sales}}</text>
						</view>
						<view class="kpi-card large">
							<view class="kpi-header">
								<u-icon name="/static/images/index/power.svg" size="20" color="#1890ff"></u-icon>
								<text class="kpi-title">本年用电</text>
							</view>
							<text class="kpi-value">{{yearData.totalPower}}</text>
						</view>

						<view class="kpi-card large">
							<view class="kpi-header">
								<u-icon name="/static/images/index/danger.svg" size="20" color="#1890ff"></u-icon>
								<text class="kpi-title">本年整改</text>
							</view>
							<text class="kpi-value">{{yearData.hiddenDanger}}</text>
						</view>
					</view>
				</view>
			</view>
			<!-- 本月各产品产量柱状图 -->
			<view class="chart-section">
				<view class="section-title">隐患类型数据</view>
				<view class="chart-card">
					<view class="chart-container">
						<hazardTypeVue :year="showTime"></hazardTypeVue>
					</view>
				</view>
			</view>
			<view class="chart-section">
				<view class="section-title">月度隐患</view>
				<view class="chart-card">
					<view class="chart-container">
						<monthHazard :year="showTime"></monthHazard>
					</view>
				</view>
			</view>
			<view class="chart-section">
				<view class="section-title">每月生产数据</view>
				<view class="chart-card">
					<view class="chart-container">
						<monthProduct :year="showTime"></monthProduct>
					</view>
				</view>
			</view>
			<view class="chart-section">
				<view class="section-title">每月销售数据</view>
				<view class="chart-card">
					<view class="chart-container">
						<monthSale :year="showTime"></monthSale>
					</view>
				</view>
			</view>
			<!-- 本月各产品产量分布饼图 -->
			<view class="chart-section">
				<view class="section-title">每个车间用电饼状图</view>
				<view class="chart-card">
					<view class="chart-container">
						<distributionVue :year="showTime"></distributionVue>
					</view>
				</view>
			</view>
			<!-- 环境监测数据 -->
			<view class="environment-section">
				<view class="section-title">安全环保月度巡检折线图</view>
				<view class="chart-container large">
					<environmentVue :year="showTime"></environmentVue>
				</view>
			</view>
		</scroll-view>
		<u-picker :show="show" :defaultIndex="[defaultIndex]" :columns="yearColumns" @confirm="onYearConfirm"
			@cancel="onYearCancel"></u-picker>
	</view>
</template>

<script>
	import hazardTypeVue from './index/cnps/hazardType.vue'
	import monthHazard from './index/cnps/monthHazard.vue'
	import monthProduct from './index/cnps/monthProduct.vue'
	import monthSale from './index/cnps/monthSale.vue'
	import distributionVue from './index/cnps/distribution.vue'
	import environmentVue from './index/cnps/environment.vue'
	import {
		totalProduction,
		totalSales,
		totalPower,
		hiddenDanger
	} from '@/api/index.js'
	import {
		formatTimestamp
	} from '@/utils/common.js'
	export default {
		components: {
			hazardTypeVue,
			monthHazard,
			monthProduct,
			monthSale,
			environmentVue,
			distributionVue
		},
		data() {
			return {
				yearData: {
					product: '',
					sales: '',
					totalPower: '',
					hiddenDanger: '',

				},
				show: false,
				selectedYear: '',
				yearColumns: [],
				showTime: '',
				defaultIndex: 0
			}
		},
		mounted() {
			this.initYearData()
		},
		onShow() {
			this.initData()
			this.showTime = this.getCurrentYear()
		},
		methods: {
			initData() {
				totalProduction(this.showTime).then(res => {
					this.yearData.product = res.msg;
				})
				totalSales(this.showTime).then(res => {
					this.yearData.sales = res.msg;
				})
				totalPower(this.showTime).then(res => {
					this.yearData.totalPower = res.msg;
				})
				hiddenDanger(this.showTime).then(res => {
					this.yearData.hiddenDanger = res.msg;
				})
			},
			// 初始化年份数据
			initYearData() {
				const currentYear = new Date().getFullYear();
				const years = [];
				// 生成1900-2100年的数据
				for (let year = 2000; year <= 2060; year++) {
					years.push(year);
				}
				//
				this.yearColumns = [years];
				this.defaultIndex = years.findIndex(item => item === currentYear)
			},

			onYearConfirm(value) {
				this.selectedYear = value.value[0];
				this.show = false;
				this.showTime = value.value[0]
				this.defaultIndex = this.yearColumns[0].findIndex(item => item === this.showTime)
				this.initData()
			},

			onYearCancel() {
				this.show = false;
			},
			selectDate() {
				this.show = true;
			},
			getCurrentDate() {
				const now = new Date();
				const year = now.getFullYear();
				const month = (now.getMonth() + 1).toString().padStart(2, '0');
				const day = now.getDate().toString().padStart(2, '0');

				return `${year}/${month}/${day}`;
			},
			getCurrentYear() {
				return new Date().getFullYear();
			}

		}
	}
</script>

<style lang="scss" scoped>
	@import url('@/static/font/navBar.less');

	.safety-analysis-page {
		// background-color: #f8f9fa;
		background: #edf0f8;
		min-height: 100vh;
		position: relative;

		.head-bg {
			width: 100%;
			position: absolute;
			left: 0;
			top: 0;
		}

		// background: linear-gradient(360deg, rgba(237, 240, 248, 0) 0%, #97C7FF 100%, #5F93FF 100%);
	}

	.header {
		display: flex;
		justify-content: space-between;
		padding: 0 24rpx;

		.header-left {
			>text:nth-child(1) {
				font-weight: 600;
				font-size: 36rpx;
				color: #333;
			}

			>text:nth-child(2) {
				color: #313D4B;
				font-size: 26rpx;
				margin-left: 5rpx;
			}
		}

		.header-right {
			background-color: #C4D4FA;
			display: flex;
			justify-content: space-around;
			flex-direction: column;
			align-items: flex-end;
			padding: 0 20rpx;
			height: 60rpx;
			border-radius: 40rpx;
		}

		.update-time {
			font-size: 20rpx;
			color: #666;
		}

		.date-selector {
			display: flex;
			align-items: center;
			gap: 8rpx;

			.date-text {
				font-size: 24rpx;
				color: #333;
			}
		}
	}





	.page-content {
		height: calc(100vh - 88rpx);
		position: absolute;
		width: 100%;
		// padding: 20rpx 30rpx;
	}

	.kpi-section {
		padding: 30rpx 24rpx;
		box-sizing: border-box;
		// margin-bottom: 30rpx;

		.bg-box {
			// border: 2rpx solid #fff;
			height: 164rpx;
			border-radius: 20rpx;
			margin-bottom: 20rpx;
			position: relative;

			.index-bg {
				width: 100%;
				height: 100%;
				position: absolute;
				left: 0;
				top: 0;
			}
		}
	}

	.kpi-row-head {
		width: 100%;
		padding: 40rpx 20rpx;
		box-sizing: border-box;
		height: 100%;
		display: flex;
		align-items: center;
		position: absolute;
		// background-color: rgba(255, 255, 255, 0.1);
		justify-content: space-around;

		.kpi-card {
			height: 100%;
			// display: flex;
			// flex: 1;
			flex-wrap: wrap;
			align-items: center;
			justify-content: center;

			.kpi-header {
				display: flex;
				align-items: center;
				justify-content: center;
				margin-bottom: 10rpx;

				.kpi-title {
					color: rgba(51, 79, 103, 0.85);
					margin-left: 10rpx;
					font-size: 24rpx;
				}

			}

			.kpi-value {
				display: flex;
				align-items: center;
				justify-content: center;
				font-weight: 600;
				font-size: 30rpx;
				color: rgba(0, 0, 0, 0.9);
			}
		}
	}

	.kpi-row {
		display: flex;
		gap: 15rpx;
		margin-bottom: 20rpx;

		.kpi-card {
			height: 152rpx;
			box-sizing: border-box;
			position: relative;
			// display: flex;
			align-content: space-around;

			// box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
			>view:first-child {
				height: 152rpx;
				border-radius: 20rpx;
				padding: 24rpx 0 24rpx 10rpx;
				position: absolute;
				z-index: 2;
				width: 100%;
				// padding: 20rpx;
				box-sizing: border-box;
				display: flex;
				align-content: space-around;
				flex-wrap: wrap;
			}

			>image {
				height: 100%;
				position: absolute;
				left: 0;
				top: 0;
			}



			&.large {
				flex: 1;
				display: flex;
				flex-direction: column;
				align-items: center;
				text-align: left;
			}

			&.small {
				flex: 1;
				display: flex;
				flex-direction: column;
				align-items: flex-start;
				text-align: left;
			}
		}

		&:last-child {
			margin-bottom: 0;
		}

		.kpi-header {
			display: flex;
			align-items: center;
			gap: 10rpx;
			// margin-bottom: 16rpx;
		}



		.kpi-value {
			font-size: 28rpx;
			font-weight: 600;
			width: 100%;
			margin-top: 10rpx;

			// margin-bottom: 8rpx;
			>text:nth-child(2) {
				font-size: 18rpx;
				font-weight: 400;
			}
		}

		.kpi-label {
			font-size: 22rpx;
			color: #666;
			margin-bottom: 4rpx;
			width: 100%;
		}

		.kpi-subtitle {
			font-size: 18rpx;
			width: 100%;
			color: rgba(0, 0, 0, 0.6);
		}
	}


	.environment-section {
		padding: 0 24rpx;
		margin-bottom: 30rpx;
		padding-bottom: 130rpx;

		.chart-container {
			padding: 30rpx 0;
			background-color: #fff;
			border-radius: 20rpx;
			margin-top: 30rpx;
		}
	}

	.environment-legend {
		display: flex;
		justify-content: space-around;
		margin-top: 20rpx;
	}

	.legend-dot {
		width: 12rpx;
		height: 12rpx;
		border-radius: 50%;
		margin-right: 8rpx;
	}


	.chart-section {
		padding: 0 24rpx;
		margin-bottom: 30rpx;
	}

	.section-title {
		font-size: 32rpx;
		font-weight: 600;
		color: #333;
		margin-bottom: 20rpx;
	}

	.chart-card {
		background-color: #fff;
		border-radius: 16rpx;
		padding: 30rpx;
		box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.chart-container {
		width: 100%;
		// height: 400rpx;
		// margin-bottom: 30rpx;
	}
</style>